package com.pgcomb.poweriot.alarmservice.global;

import com.pgcomb.common.bean.ResultBean;
import com.pgcomb.common.enums.GlobalCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.UnsatisfiedServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import java.nio.file.AccessDeniedException;

/**
 * @Description 异常统一处理
 * @Author zhengheqi
 * @Date 2020/4/10 17:31
 * @Version 1.0.0
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandlerAdvice {

    @ExceptionHandler({IllegalArgumentException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResultBean<Object> badRequestException(IllegalArgumentException exception) {
        log.error(exception.getMessage(),exception);
        return ResultBean.error(GlobalCodeEnum.INVALID_PARAMS.getCode() + "", exception.getMessage());
    }

    @ExceptionHandler({AccessDeniedException.class})
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ResultBean<Object> badRequestException(AccessDeniedException exception) {
        log.error(exception.getMessage(),exception);
        return ResultBean.error(HttpStatus.FORBIDDEN.value() + "", exception.getMessage());
    }
//
//    @ExceptionHandler({MissingServletRequestParameterException.class, HttpMessageNotReadableException.class,
//            UnsatisfiedServletRequestParameterException.class, MethodArgumentTypeMismatchException.class})
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
//    public ResultBean<Object> badRequestException(Exception exception) {
//        log.error(exception.getMessage(),exception);
//        return ResultBean.error(HttpStatus.BAD_REQUEST.value() + "", exception.getMessage());
//    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResultBean<Object> exception(Exception e) {
        log.error("alarm-service error", e);

        return ResultBean.error(GlobalCodeEnum.UNKNOW_ERROR.getCode() + "", e.getMessage());

    }


}
